package xyz.renyumeng.xkm.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 全局统一返回结果类
 *
 * @author renyumeng
 * @date Created in 23:47 2022/10/22
 */

@Data
@NoArgsConstructor
@ApiModel("全局统一返回结果")
public class Result<T> {

    /**
     * 状态码
     */
    @ApiModelProperty(value = "返回码")
    private Integer code;

    /**
     * 返回状态信息
     */
    @ApiModelProperty(value = "返回消息")
    private String message;


    /**
     * 返回数据
     */
    @ApiModelProperty(value = "返回数据")
    private T data;


    /**
     * 修改数据
     *
     * @param body    身体
     * @param code    状态码
     * @param message 信息
     * @return {@link Result}<{@link T}>
     */
    public static <T> Result<T> build(T body, Integer code, String message) {
        Result<T> result = new Result<>();
        if (body != null) {
            result.setData(body);
        }
        result.setCode(code);
        result.setMessage(message);
        return result;

    }


    public static <T> Result<T> ok() {
        return Result.ok(null);
    }

    public static <T> Result<T> fail() {
        return Result.fail(null);
    }

    /**
     * 操作成功
     *
     * @param data 数据
     * @return {@link Result}<{@link T}>
     */
    public static <T> Result<T> ok(T data) {
        ResultCodeEnum success = ResultCodeEnum.SUCCESS;
        return Result.build(data, success.getCode(), success.getMessage());


    }

    /**
     * 操作失败
     *
     * @param data 数据
     * @return {@link Result}<{@link T}>
     */
    public static <T> Result<T> fail(T data) {
        ResultCodeEnum fail = ResultCodeEnum.FAIL;
        return Result.build(data, fail.getCode(), fail.getMessage());
    }

    public Result<T> message(String message) {
        this.setMessage(message);
        return this;
    }

    public Result<T> code(Integer code) {
        this.setCode(code);
        return this;
    }

}
